let app = getApp()
var {
  Api
} = require('../../api/api')
Component({
  data: {
    userInfo: {}, // 用户信息

    domain: Api.domain,
  },
  properties: {
    // 登录状态
    active: {
      type: Boolean,
      value: false
    },

  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      console.log("2222222222222");
      this.jinglogo();
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
      console.log("33333333332");
    },
  },


  pageLifetimes: {
    show: function() {
      // 页面被展示
      console.log("444444444444");
    },
    hide: function() {
      // 页面被隐藏
      console.log("55555555555555");
    },
    resize: function(size) {
      // 页面尺寸变化
      console.log("6666666666666");
    }
  },

  /*
  onShow: function () {
    
    thisPage.setData({
      register:false
    })
    thisPage.onShow();
  },
  */
  methods: {
    //静默登录
    jinglogo(){
      try {
        console.log(wx.getUserProfile,'xxx')
        if(!wx.getUserProfile){//电脑登录
          console.log('电脑登录')
          this.diannao()
          return
        }
       
            wx.login({
              success: ras => {

                console.log(ras,'第一1111111111111111111111111次')

                var pamdata = {
                  
                  code: ras.code,
                  question_id:wx.getStorageSync('question_id') || 0,
                  p_id: wx.$cache.get('pid') || 0
                };
                wx.$http.post(wx.$get.getopenid, pamdata).then(res => {
                  console.log(res,'第一次')
                  wx.$cache.set('key', res.token)
                  // this.user()
                  this.dengtlu()
                })
              }
            })
          
          
        
      } catch (e) {
          wx.showToast({
            title: '请更新微信版本',
            icon:'none'
          })
      }
    },


    // 授权
    bindInfo() {
      try {
        console.log(wx.getUserProfile,'xxx')
        if(!wx.getUserProfile){//电脑登录
          console.log('电脑登录')
          this.diannao()
          return
        }
        wx.getUserProfile({
          desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
            console.log(res)
            this.setData({
              userInfo: res.userInfo,
            })
            wx.login({
              success: ras => {
                var pamdata = {
                  code: ras.code,
                  question_id:wx.getStorageSync('question_id') || 0,
                  p_id: wx.$cache.get('pid') || 0
                };
                wx.$http.post(wx.$get.getopenid, pamdata).then(res => {
                  console.log(res,'第一次')
                  wx.$cache.set('key', res.token)
                  // this.user()
                  this.dengtlu()
                })
              }
            })
          },
          fail:(err)=>{
            console.log(err,'失败')
          }
        })
      } catch (e) {
          wx.showToast({
            title: '请更新微信版本',
            icon:'none'
          })
      }
    },
    diannao(){
      wx.login({
        success: ras => {
          var pamdata = {
            code: ras.code,
            p_id: wx.$cache.get('pid') || 0,
            question_id:wx.getStorageSync('question_id') || 0,

          };
          wx.$http.post(wx.$get.getopenid, pamdata).then(res => {
            console.log(res,'第一次')
            wx.$cache.set('key', res.token)
            this.user()
          })
        }
      })
    },
    dengtlu() {
      let uerinfo = this.data.userInfo
      let params = {
        nickname:uerinfo.nickName,
        img:uerinfo.avatarUrl,
        sex:uerinfo.gender
      }
      /*
      wx.$http.get(wx.$get.update_weixin_member,params).then(res=>{
        console.log(res,'更新头像')
        this.user()
        // var pageArr = getCurrentPages();
        // var thisPage = pageArr[pageArr.length - 1];
        // thisPage.setData({
        //   register:false
        // })
        // thisPage.onShow()
      })
      */
    },
    user(){
      wx.$http.get(wx.$get.get_member_info,{}).then(res=>{
        if(!res.vip_time){
          res.vip_time='1970-01-01'
        }
        console.log(res,'用户信息')
        wx.$cache.set('userinfo',res)
        wx.$cache.set('member_id',res.id)
        var pageArr = getCurrentPages();
        var thisPage = pageArr[pageArr.length - 1];
        thisPage.setData({
          register:false
        })
        thisPage.onShow()
        wx.login({
          success(res){
            app.code = res.code
          }
        })
      })
    },
    // 取消授权
    closethis1() {
      this.setData({
        active: false
      })
      wx.reLaunch({
        url: '/pages/index/index',
      })
    },

    closeyanzheng() {
      this.closethis1()
    }
  },
})
